import * as PIXI from 'pixi.js';
import MsgService from './msg-service';
// 图片服务
export default class ImagesService {
  private loader = new PIXI.Loader();
  resources = new Map<string, PIXI.Sprite>();
  constructor(private ms: MsgService) { }
  add(...images: string[]) {
    return new Promise<Map<string, PIXI.Sprite>>((resolve, reject) => {
      const arr: string[] = []
      for (const i of images) {
        if (!this.resources.has(i)) {
          arr.push(i)
        }
      }
      if (!arr.length) {
        resolve(this.resources);
        return
      }
      this.loader.add(arr);
      this.loader.load((r) => {
        for (const i of arr) {
          const s = new PIXI.Sprite(this.loader.resources[i].texture);
          this.resources.set(i, s);
          this.ms.toast(`加载图像: ${i}`)
        }
        resolve(this.resources);
      })
    })
  }
  async getSprite(image: string) {
    const r = await this.add(image);
    return r.get(image)
  }
}
